const app = getApp();

Page({
  data: {
    //商品列表
    product_list: [],
    //分页参数
    params: {
      has_more: true,
      cursor: "",
      count: 20,
    },
    //查看资质窗口
    popupshow: false,
    url: "",
  },
  onLoad: function () {
    let that = this;
    new Promise((resolve: any, reject: any) => {
      tt.request({
        url: getApp().globalData.baseurl + "/douyin/query_service_provider",
        data: {},
        header: {
          "content-type": "application/json",
        },
        method: "POST",
        responseType: "text",
        success(res: any) {
          if (res.data.code === 0) {
            resolve(res.data.data);
          } else {
            reject("请求报错");
          }
        },
        fail(err: any) {
          reject(err.errMsg);
        },
      });
    }).then((res: any) => {
      that.setData({
        url: res.business_license.url,
      });
    });
    that.Login();
    that.getProductlist();
  },
  //查看详情
  godeltis(event: any) {
    tt.navigateTo({
      url:
        "/pages/productdetails/productdetails?productid=" +
        event.currentTarget.dataset.productid,
    });
  },
  //登录
  Login() {
    if (!getApp().globalData.userinfo) {
      new Promise((resolve: any, reject: any) => {
        tt.login({
          force: true,
          success(res) {
            let { code } = res;
            tt.request({
              url: getApp().globalData.baseurl + "/douyin/code2session",
              data: {
                code: code,
              },
              header: {
                "content-type": "application/json",
              },
              method: "GET",
              responseType: "text",
              success(res: any) {
                if (res.data.code === 0) {
                  resolve(res.data);
                } else {
                  reject("请求报错");
                }
              },
              fail(err: any) {
                reject(err.errMsg);
              },
            });
          },
          fail() {
            reject("登陆失败");
          },
        });
      })
        .then((res: any) => {
          getApp().globalData.userinfo = res.data;
          console.log(res.data)
          console.log("登陆成功");
        })
        .catch((err: any) => {
          tt.showToast({
            title: err,
            icon: "fail",
            duration: 2000,
          });
        });
    }
  },
  //获取商品列表
  getProductlist() {
    let that = this;
    //判断是否还有更多
    if (that.data.params.has_more) {
      new Promise((resolve: any, reject: any) => {
        tt.request({
          url: getApp().globalData.baseurl + "/douyin/query_product_list",
          data: that.data.params,
          header: {
            "content-type": "application/json",
          },
          method: "POST",
          responseType: "text",
          success(res: any) {
            if (res.data.code === 0) {
              resolve(res.data.data);
            } else {
              reject("请求报错");
            }
          },
          fail(err: any) {
            reject(err.errMsg);
          },
        });
      })
        .then((res: any) => {
          let products_list: any = [];
          res.products.forEach((item: any) => {
            products_list.push({
              productitem: item,
              discount: (
                (item.sku.actual_amount / item.sku.origin_amount) *
                10
              ).toFixed(2),
              imglist: JSON.parse(item.product.attr_key_value_map.image_list),
            });
          });
          that.setData({
            params: {
              ...that.data.params,
              has_more: res.has_more,
              cursor: res.next_cursor,
            },
            product_list: products_list,
          });
        })
        .catch((err: any) => {
          tt.showToast({
            title: err,
            icon: "fail",
            duration: 2000,
          });
        });
    }
  },
  //触底事件
  scroolLower() {
    let that = this;
    that.getProductlist();
  },
  //查看资质
  checkQualif() {
    let that = this;
    that.setData({
      popupshow: true,
    });
  },
  //关闭弹出层
  closePopup() {
    let that = this;
    that.setData({
      popupshow: false,
    });
  },
});
